﻿/*
 * FileName: BowlingEngineAI.cs
 * Module: Core
 * Author: Gourav Das
 * Created: 06 June 2011
 * Modifications:
 * Copyright: Gourav Das, 2011
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using _22yards.EnumDeclarations;
using _22yards.Core.MatchPlayEngine;

namespace _22yards.Core.BowlingEngineAI
{
    public class BowlingEngineAI
    {
        private static BowlingEngineAI SingletonInstance;
        private short bowlSkill;
        private cPlayer BowlerBowling;
        private Team BowlingTeam;
        private Match match;
        private short FastBallProbability;
        private short SlowBallProbability;

        private List<short> DefaultBowlingRegionAllBowlers;
        private List<short> DefaultBowlingWideRegionList;
        private List<short> DefaultBowlingRegionList;

        private Random rnd;

        private BowlingEngineAI() 
        {
            DefaultBowlingRegionAllBowlers = new List<short>();
            rnd                            = new Random();

            DefaultBowlingWideRegionList = new List<short>();
            DefaultBowlingRegionList     = new List<short>();

            #region BuildDefaultBowlingBowlingRegion
            for (short i = 1; i <= 297; i++)
            {
                #region Default Fill
                if (i <= 9)
                    DefaultBowlingRegionAllBowlers.Add(i); //1-9
                else if (i >= 10 && i <= 18)
                    DefaultBowlingRegionAllBowlers.Add(i); //10-18
                else if (i >= 19 && i <= 27)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 19 + 10));//10-18
                else if (i >= 28 && i <= 36)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 28 + 19)); //19-27
                else if (i >= 37 && i <= 45)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 28 + 10)); //19-27
                else if (i >= 46 && i <= 54)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 37 + 19)); //28-36
                else if (i >= 55 && i <= 63)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 37 + 10)); //28-36
                else if (i >= 64 && i <= 72)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 46 + 19)); //37-45
                else if (i >= 73 && i <= 81)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 46 + 10)); //37-45
                else if (i >= 82 && i <= 90)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 55 + 19)); //46-54
                else if (i >= 91 && i <= 99)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 55 + 10)); //46-54
                else if (i >= 100 && i <= 108)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 64 + 19)); //55-63
                else if (i >= 109 && i <= 117)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 64 + 10)); //55-63
                else if (i >= 118 && i <= 126)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 73 + 19)); //64-72
                else if (i >= 127 && i <= 135)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 73 + 10)); //64-72
                else if (i >= 136 && i <= 144)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 82 + 19)); //73 - 81
                else if (i >= 145 && i <= 153)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 82 + 10)); //73-81
                else if (i >= 154 && i <= 162)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 91 + 19)); //82-90
                else if (i >= 163 && i <= 171)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 91 + 10)); //82-90
                else if (i >= 172 && i <= 180)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 100 + 19)); //91-99
                else if (i >= 181 && i <= 189)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 100 + 10)); //91-99
                else if (i >= 190 && i <= 198)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 109 + 19)); //100-108
                else if (i >= 199 && i <= 207)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 109 + 19)); //109-117
                else if (i >= 208 && i <= 213)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 207 + 19)); //20-25
                else if (i >= 214 && i <= 216)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 211 + 19)); //22-24
                else if (i >= 217 && i <= 222)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 207 + 19)); //29-34
                else if (i >= 223 && i <= 225)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 212 + 19)); //30-33
                else if (i >= 226 && i <= 231)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 207 + 19)); //38-43
                else if (i >= 232 && i <= 234)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 212 + 19)); //39-42
                else if (i >= 235 && i <= 240)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 207 + 19)); //47-52
                else if (i >= 241 && i <= 243)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 212 + 19)); //48-51
                else if (i >= 244 && i <= 249)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 207 + 19)); //56-61
                else if (i >= 250 && i <= 252)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 212 + 19)); //57-60
                else if (i >= 253 && i <= 258)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 207 + 19)); //65-70
                else if (i >= 259 && i <= 261)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 212 + 19)); //66-69
                else if (i >= 262 && i <= 267)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 207 + 19)); //74-79
                else if (i >= 268 && i <= 270)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 212 + 19)); //75-78
                else if (i >= 271 && i <= 276)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 207 + 19)); //83-88
                else if (i >= 277 && i <= 279)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 212 + 19)); //84-87
                else if (i >= 280 && i <= 285)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 207 + 19)); //92-97
                else if (i >= 286 && i <= 288)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 212 + 19)); //93-96
                else if (i >= 289 && i <= 294)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 207 + 19)); //101-106
                else if (i >= 295 && i <= 297)
                    DefaultBowlingRegionAllBowlers.Add(Convert.ToInt16(i % 212 + 19)); //102-105
                #endregion
            }
            #endregion
        }

        public static BowlingEngineAI Init()
        {
            if (SingletonInstance == null)
                SingletonInstance = new BowlingEngineAI();
            
            return SingletonInstance;
        }

        /// <summary>
        /// Initializes the required lists and variables for the current over
        /// </summary>
        /// <param name="_BattingTeam">The batting team.</param>
        /// <param name="_BowlingTeam">The bowling team.</param>
        /// <param name="_match">The Match.</param>
        /// <returns></returns>
        public bool Begin(ref Team _BowlingTeam, ref Match _match)
        {
            if (_BowlingTeam == null || _match == null)
            {
                BowlerBowling = null;
                BowlingTeam = null;
                match = null;
                return false;
            }

            if (_BowlingTeam.players.First(c => c.IsBowlingCurrently == true) == null)
            {
                BowlerBowling = null;
                BowlingTeam = null;
                match = null;
                return false;
            }

            match       = _match;
            BowlingTeam = _BowlingTeam;

            BowlerBowling = BowlingTeam.players.First(c => c.IsBowlingCurrently == true);
            bowlSkill     = BowlerBowling.BowlingSkillPoints;

            FastBallProbability = Convert.ToInt16(((float)BowlerBowling.FastBowlingPoints * 100 / (float)bowlSkill));
            SlowBallProbability = Convert.ToInt16(((float)BowlerBowling.SpinBowlingPoints * 100 / (float)bowlSkill));

            DefaultBowlingRegionList.Clear();
            DefaultBowlingWideRegionList.Clear();

            if (BowlerBowling.BowlerType == (short)BowlerType.Fast ||
                BowlerBowling.BowlerType == (short)BowlerType.FastMedium ||
                BowlerBowling.BowlerType == (short)BowlerType.MediumFast)
            {
                //42 Items
                DefaultBowlingRegionList.Add(47);
                DefaultBowlingRegionList.Add(48);
                DefaultBowlingRegionList.Add(49);
                DefaultBowlingRegionList.Add(50);
                DefaultBowlingRegionList.Add(51);
                DefaultBowlingRegionList.Add(52);
                DefaultBowlingRegionList.Add(56);
                DefaultBowlingRegionList.Add(57);
                DefaultBowlingRegionList.Add(58);
                DefaultBowlingRegionList.Add(59);
                DefaultBowlingRegionList.Add(60);
                DefaultBowlingRegionList.Add(61);
                DefaultBowlingRegionList.Add(65);
                DefaultBowlingRegionList.Add(66);
                DefaultBowlingRegionList.Add(67);
                DefaultBowlingRegionList.Add(68);
                DefaultBowlingRegionList.Add(69);
                DefaultBowlingRegionList.Add(70);
                DefaultBowlingRegionList.Add(74);
                DefaultBowlingRegionList.Add(75);
                DefaultBowlingRegionList.Add(76);
                DefaultBowlingRegionList.Add(77);
                DefaultBowlingRegionList.Add(78);
                DefaultBowlingRegionList.Add(82);
                DefaultBowlingRegionList.Add(83);
                DefaultBowlingRegionList.Add(84);
                DefaultBowlingRegionList.Add(85);
                DefaultBowlingRegionList.Add(86);
                DefaultBowlingRegionList.Add(87);
                DefaultBowlingRegionList.Add(88);
                DefaultBowlingRegionList.Add(92);
                DefaultBowlingRegionList.Add(93);
                DefaultBowlingRegionList.Add(94);
                DefaultBowlingRegionList.Add(95);
                DefaultBowlingRegionList.Add(96);
                DefaultBowlingRegionList.Add(97);
                DefaultBowlingRegionList.Add(101);
                DefaultBowlingRegionList.Add(102);
                DefaultBowlingRegionList.Add(103);
                DefaultBowlingRegionList.Add(104);
                DefaultBowlingRegionList.Add(105);
                DefaultBowlingRegionList.Add(106);

                //24 items
                DefaultBowlingWideRegionList.Add(10);
                DefaultBowlingWideRegionList.Add(19);
                DefaultBowlingWideRegionList.Add(28);
                DefaultBowlingWideRegionList.Add(37);
                DefaultBowlingWideRegionList.Add(46);
                DefaultBowlingWideRegionList.Add(55);
                DefaultBowlingWideRegionList.Add(64);
                DefaultBowlingWideRegionList.Add(73);
                DefaultBowlingWideRegionList.Add(82);
                DefaultBowlingWideRegionList.Add(91);
                DefaultBowlingWideRegionList.Add(100);
                DefaultBowlingWideRegionList.Add(109);
                DefaultBowlingWideRegionList.Add(17);
                DefaultBowlingWideRegionList.Add(26);
                DefaultBowlingWideRegionList.Add(35);
                DefaultBowlingWideRegionList.Add(44);
                DefaultBowlingWideRegionList.Add(53);
                DefaultBowlingWideRegionList.Add(62);
                DefaultBowlingWideRegionList.Add(71);
                DefaultBowlingWideRegionList.Add(80);
                DefaultBowlingWideRegionList.Add(89);
                DefaultBowlingWideRegionList.Add(98);
                DefaultBowlingWideRegionList.Add(107);
                DefaultBowlingWideRegionList.Add(116);
            }
            else if(BowlerBowling.BowlerType == (short)_22yards.EnumDeclarations.BowlerType.SlowMedium)
            {
                //30 items
                DefaultBowlingRegionList.Add(47);
                DefaultBowlingRegionList.Add(48);
                DefaultBowlingRegionList.Add(49);
                DefaultBowlingRegionList.Add(50);
                DefaultBowlingRegionList.Add(51);
                DefaultBowlingRegionList.Add(52);
                DefaultBowlingRegionList.Add(56);
                DefaultBowlingRegionList.Add(57);
                DefaultBowlingRegionList.Add(58);
                DefaultBowlingRegionList.Add(59);
                DefaultBowlingRegionList.Add(60);
                DefaultBowlingRegionList.Add(61);
                DefaultBowlingRegionList.Add(65);
                DefaultBowlingRegionList.Add(66);
                DefaultBowlingRegionList.Add(67);
                DefaultBowlingRegionList.Add(68);
                DefaultBowlingRegionList.Add(69);
                DefaultBowlingRegionList.Add(70);
                DefaultBowlingRegionList.Add(74);
                DefaultBowlingRegionList.Add(75);
                DefaultBowlingRegionList.Add(76);
                DefaultBowlingRegionList.Add(77);
                DefaultBowlingRegionList.Add(78);
                DefaultBowlingRegionList.Add(79);
                DefaultBowlingRegionList.Add(83);
                DefaultBowlingRegionList.Add(84);
                DefaultBowlingRegionList.Add(85);
                DefaultBowlingRegionList.Add(86);
                DefaultBowlingRegionList.Add(87);
                DefaultBowlingRegionList.Add(88);

                //18 items
                DefaultBowlingWideRegionList.Add(10);
                DefaultBowlingWideRegionList.Add(19);
                DefaultBowlingWideRegionList.Add(28);
                DefaultBowlingWideRegionList.Add(37);
                DefaultBowlingWideRegionList.Add(46);
                DefaultBowlingWideRegionList.Add(55);
                DefaultBowlingWideRegionList.Add(64);
                DefaultBowlingWideRegionList.Add(73);
                DefaultBowlingWideRegionList.Add(82);
                DefaultBowlingWideRegionList.Add(17);
                DefaultBowlingWideRegionList.Add(26);
                DefaultBowlingWideRegionList.Add(35);
                DefaultBowlingWideRegionList.Add(44);
                DefaultBowlingWideRegionList.Add(53);
                DefaultBowlingWideRegionList.Add(62);
                DefaultBowlingWideRegionList.Add(71);
                DefaultBowlingWideRegionList.Add(80);
                DefaultBowlingWideRegionList.Add(89);
            }
            else if (BowlerBowling.BowlerType == (short)_22yards.EnumDeclarations.BowlerType.OffSpin)
            {
                //20 items
                DefaultBowlingRegionList.Add(47);
                DefaultBowlingRegionList.Add(48);
                DefaultBowlingRegionList.Add(49);
                DefaultBowlingRegionList.Add(50);
                DefaultBowlingRegionList.Add(51);
                DefaultBowlingRegionList.Add(56);
                DefaultBowlingRegionList.Add(57);
                DefaultBowlingRegionList.Add(58);
                DefaultBowlingRegionList.Add(59);
                DefaultBowlingRegionList.Add(60);
                DefaultBowlingRegionList.Add(65);
                DefaultBowlingRegionList.Add(66);
                DefaultBowlingRegionList.Add(67);
                DefaultBowlingRegionList.Add(68);
                DefaultBowlingRegionList.Add(69);
                DefaultBowlingRegionList.Add(74);
                DefaultBowlingRegionList.Add(75);
                DefaultBowlingRegionList.Add(76);
                DefaultBowlingRegionList.Add(77);
                DefaultBowlingRegionList.Add(78);
                
                //16 items
                DefaultBowlingWideRegionList.Add(10);
                DefaultBowlingWideRegionList.Add(19);
                DefaultBowlingWideRegionList.Add(28);
                DefaultBowlingWideRegionList.Add(37);
                DefaultBowlingWideRegionList.Add(46);
                DefaultBowlingWideRegionList.Add(55);
                DefaultBowlingWideRegionList.Add(64);
                DefaultBowlingWideRegionList.Add(73);
                DefaultBowlingWideRegionList.Add(17);
                DefaultBowlingWideRegionList.Add(26);
                DefaultBowlingWideRegionList.Add(35);
                DefaultBowlingWideRegionList.Add(44);
                DefaultBowlingWideRegionList.Add(53);
                DefaultBowlingWideRegionList.Add(62);
                DefaultBowlingWideRegionList.Add(79);
                DefaultBowlingWideRegionList.Add(80);
            }
            else if (BowlerBowling.BowlerType == (short)_22yards.EnumDeclarations.BowlerType.LegSpin)
            {
                //20 items
                DefaultBowlingRegionList.Add(48);
                DefaultBowlingRegionList.Add(49);
                DefaultBowlingRegionList.Add(50);
                DefaultBowlingRegionList.Add(51);
                DefaultBowlingRegionList.Add(52);
                DefaultBowlingRegionList.Add(57);
                DefaultBowlingRegionList.Add(58);
                DefaultBowlingRegionList.Add(59);
                DefaultBowlingRegionList.Add(60);
                DefaultBowlingRegionList.Add(61);
                DefaultBowlingRegionList.Add(66);
                DefaultBowlingRegionList.Add(67);
                DefaultBowlingRegionList.Add(68);
                DefaultBowlingRegionList.Add(69);
                DefaultBowlingRegionList.Add(70);
                DefaultBowlingRegionList.Add(76);
                DefaultBowlingRegionList.Add(77);
                DefaultBowlingRegionList.Add(78);
                DefaultBowlingRegionList.Add(79);
                DefaultBowlingRegionList.Add(80);

                //20 items
                DefaultBowlingWideRegionList.Add(10);
                DefaultBowlingWideRegionList.Add(19);
                DefaultBowlingWideRegionList.Add(28);
                DefaultBowlingWideRegionList.Add(37);
                DefaultBowlingWideRegionList.Add(46);
                DefaultBowlingWideRegionList.Add(47);
                DefaultBowlingWideRegionList.Add(55);
                DefaultBowlingWideRegionList.Add(56);
                DefaultBowlingWideRegionList.Add(64);
                DefaultBowlingWideRegionList.Add(65);
                DefaultBowlingWideRegionList.Add(73);
                DefaultBowlingWideRegionList.Add(74);
                DefaultBowlingWideRegionList.Add(75);
                DefaultBowlingWideRegionList.Add(17);
                DefaultBowlingWideRegionList.Add(26);
                DefaultBowlingWideRegionList.Add(35);
                DefaultBowlingWideRegionList.Add(44);
                DefaultBowlingWideRegionList.Add(53);
                DefaultBowlingWideRegionList.Add(62);
                DefaultBowlingWideRegionList.Add(71);
            }

            return true;
        }

        public short GenerateBallRegion(ref Team _BattingTeam, double _MRF)
        {
            if (_BattingTeam == null || BowlerBowling == null)
                return -1;
            List<short> bowlRegion           = new List<short>(100);
            cPlayer BatsmanFacing            = _BattingTeam.players.First(c => c.IsOnStrike == true);

            #region Calculate Bowling Skill for this ball

            short bowlSkillFunctionTemp = bowlSkill; //bowlSkill for this function call
            bowlSkillFunctionTemp       = (short)((bowlSkillFunctionTemp + BowlerBowling.CareerBowlingAura) / 2);
            bowlSkillFunctionTemp       = Convert.ToInt16(((float)bowlSkillFunctionTemp + (float)BowlerBowling.BowlingSkillPoints + (float)BowlerBowling.CurrentBowlingConfidence) / 2.8);

            switch (match.matchScore.CurrentInningsScore().RunConceededBowlerPreviousBall(BowlerBowling.PlayerID))
            {
                case 0:
                    bowlSkillFunctionTemp = Convert.ToInt16(((float)bowlSkillFunctionTemp + (float)BowlerBowling.Volatility * .1f));
                    break;
                case 2:
                    bowlSkillFunctionTemp = Convert.ToInt16(((float)bowlSkillFunctionTemp - (float)BowlerBowling.Volatility * .06f));
                    break;
                case 3:
                    bowlSkillFunctionTemp = Convert.ToInt16(((float)bowlSkillFunctionTemp - (float)BowlerBowling.Volatility * .08f));
                    break;
                case 4:
                    bowlSkillFunctionTemp = Convert.ToInt16(((float)bowlSkillFunctionTemp - (float)BowlerBowling.Volatility * .12f));
                    break;
                case 5:
                    bowlSkillFunctionTemp = Convert.ToInt16(((float)bowlSkillFunctionTemp - (float)BowlerBowling.Volatility * .18f));
                    break;
                case 6:
                    bowlSkillFunctionTemp = Convert.ToInt16(((float)bowlSkillFunctionTemp - (float)BowlerBowling.Volatility * .24f));
                    break;
                case 7:
                    bowlSkillFunctionTemp = Convert.ToInt16(((float)bowlSkillFunctionTemp - (float)BowlerBowling.Volatility * .24f));
                    break;
                case 8:
                    bowlSkillFunctionTemp = Convert.ToInt16(((float)bowlSkillFunctionTemp - (float)BowlerBowling.Volatility * .24f));
                    break;
                case 9:
                    bowlSkillFunctionTemp = Convert.ToInt16(((float)bowlSkillFunctionTemp - (float)BowlerBowling.Volatility * .24f));
                    break;
            }

            if (match.matchScore.CurrentInningsScore().WicketFallenPreviousBall(BowlerBowling.PlayerID))
                bowlSkillFunctionTemp = Convert.ToInt16(((float)bowlSkillFunctionTemp + (float)BowlerBowling.Volatility * .15f));

            if (bowlSkillFunctionTemp >= 95)
                bowlSkillFunctionTemp = 95;

            #endregion

            #region Build Bowling Regions based on bowler type and above calculated bowlSkillFunctionTemp

            if (BowlerBowling.BowlerType == (short)_22yards.EnumDeclarations.BowlerType.Fast ||
                BowlerBowling.BowlerType == (short)_22yards.EnumDeclarations.BowlerType.FastMedium ||
                BowlerBowling.BowlerType == (short)_22yards.EnumDeclarations.BowlerType.MediumFast)
            {
                //Right hander to Right hander or Left hander to left hander
                if ((BatsmanFacing.IsRightHandedBatsman && BowlerBowling.IsRightHandedBowler) || (!BatsmanFacing.IsRightHandedBatsman && !BowlerBowling.IsRightHandedBowler))
                {
                    short x = Convert.ToInt16(100 - bowlSkillFunctionTemp);
                    short i = 0;

                    for (; i < x-1; i++)
                        bowlRegion.Add(DefaultBowlingRegionAllBowlers[Convert.ToInt16(rnd.Next(0, 297))]);

                    i++;

                    if (_MRF > 87)
                    {
                        bowlRegion[i++] = 20;
                        bowlRegion[i++] = 21;
                        bowlRegion[i++] = 22;
                        bowlRegion[i++] = 23;
                        bowlRegion[i++] = 24;
                        bowlRegion[i++] = 25;
                        bowlRegion[i++] = 29;
                        bowlRegion[i++] = 30;
                        bowlRegion[i++] = 31;
                        bowlRegion[i++] = 32;
                        bowlRegion[i++] = 33;
                        bowlRegion[i++] = 34;
                        bowlRegion[i++] = 38;
                        bowlRegion[i++] = 39;
                        bowlRegion[i++] = 40;
                        bowlRegion[i++] = 41;
                        bowlRegion[i++] = 42;
                        bowlRegion[i++] = 43;
                    }
                    else
                    {
                        bowlRegion[i++] = 47;
                        bowlRegion[i++] = 48;
                        bowlRegion[i++] = 49;
                        bowlRegion[i++] = 50;
                        bowlRegion[i++] = 51;
                        bowlRegion[i++] = 56;
                        bowlRegion[i++] = 57;
                        bowlRegion[i++] = 58;
                        bowlRegion[i++] = 59;
                        bowlRegion[i++] = 60;
                        bowlRegion[i++] = 65;
                        bowlRegion[i++] = 66;
                        bowlRegion[i++] = 67;
                        bowlRegion[i++] = 68;
                        bowlRegion[i++] = 69;
                        bowlRegion[i++] = 74;
                        bowlRegion[i++] = 75;
                        bowlRegion[i++] = 76;
                        bowlRegion[i++] = 77;
                        bowlRegion[i++] = 78;
                        bowlRegion[i++] = 83;
                        bowlRegion[i++] = 84;
                        bowlRegion[i++] = 85;
                        bowlRegion[i++] = 86;
                        bowlRegion[i++] = 87;
                    }

                    for (; i < 94; i++)
                        bowlRegion[i] = DefaultBowlingRegionList[rnd.Next(0, 42)];

                    for (; i < 100; i++)
                        bowlRegion[i] = DefaultBowlingWideRegionList[rnd.Next(0, 24)];

                    i = 0;

                }
                //Right hander to Left hander or Left hander to Right hander
                else if ((!BatsmanFacing.IsRightHandedBatsman && BowlerBowling.IsRightHandedBowler) || (BatsmanFacing.IsRightHandedBatsman && !BowlerBowling.IsRightHandedBowler))
                {
                    short x = Convert.ToInt16(100 - bowlSkillFunctionTemp);
                    short i = 0;

                    for (; i < x - 1; i++)
                        bowlRegion.Add(DefaultBowlingRegionAllBowlers[Convert.ToInt16(rnd.Next(0, 297))]);

                    i++;

                    if (_MRF > 87)
                    {
                        bowlRegion[i++] = 20;
                        bowlRegion[i++] = 21;
                        bowlRegion[i++] = 22;
                        bowlRegion[i++] = 23;
                        bowlRegion[i++] = 24;
                        bowlRegion[i++] = 25;
                        bowlRegion[i++] = 29;
                        bowlRegion[i++] = 30;
                        bowlRegion[i++] = 31;
                        bowlRegion[i++] = 32;
                        bowlRegion[i++] = 33;
                        bowlRegion[i++] = 34;
                        bowlRegion[i++] = 38;
                        bowlRegion[i++] = 39;
                        bowlRegion[i++] = 40;
                        bowlRegion[i++] = 41;
                        bowlRegion[i++] = 42;
                        bowlRegion[i++] = 43;
                    }
                    else
                    {
                        bowlRegion[i++] = 48;
                        bowlRegion[i++] = 49;
                        bowlRegion[i++] = 50;
                        bowlRegion[i++] = 51;
                        bowlRegion[i++] = 52;
                        bowlRegion[i++] = 57;
                        bowlRegion[i++] = 58;
                        bowlRegion[i++] = 59;
                        bowlRegion[i++] = 60;
                        bowlRegion[i++] = 61;
                        bowlRegion[i++] = 66;
                        bowlRegion[i++] = 67;
                        bowlRegion[i++] = 68;
                        bowlRegion[i++] = 69;
                        bowlRegion[i++] = 70;
                        bowlRegion[i++] = 75;
                        bowlRegion[i++] = 76;
                        bowlRegion[i++] = 77;
                        bowlRegion[i++] = 78;
                        bowlRegion[i++] = 79;
                        bowlRegion[i++] = 84;
                        bowlRegion[i++] = 85;
                        bowlRegion[i++] = 86;
                        bowlRegion[i++] = 87;
                        bowlRegion[i++] = 88;
                    }

                    for (; i < 94; i++)
                        bowlRegion[i] = DefaultBowlingRegionList[rnd.Next(0, 42)];

                    for (; i < 100; i++)
                        bowlRegion[i] = DefaultBowlingWideRegionList[rnd.Next(0, 24)];

                    i = 0;

                }

            }
            else if (BowlerBowling.BowlerType == (short)_22yards.EnumDeclarations.BowlerType.SlowMedium)
            {
                //Right hander to Right hander or Left hander to left hander
                if ((BatsmanFacing.IsRightHandedBatsman && BowlerBowling.IsRightHandedBowler) || (!BatsmanFacing.IsRightHandedBatsman && !BowlerBowling.IsRightHandedBowler))
                {
                    short x = Convert.ToInt16(100 - bowlSkillFunctionTemp);
                    short i = 0;

                    for (; i < x - 1; i++)
                        bowlRegion.Add(DefaultBowlingRegionAllBowlers[Convert.ToInt16(rnd.Next(0, 297))]);

                    i++;

                    if (_MRF > 87)
                    {
                        bowlRegion[i++] = 20;
                        bowlRegion[i++] = 21;
                        bowlRegion[i++] = 22;
                        bowlRegion[i++] = 23;
                        bowlRegion[i++] = 24;
                        bowlRegion[i++] = 25;
                        bowlRegion[i++] = 29;
                        bowlRegion[i++] = 30;
                        bowlRegion[i++] = 31;
                        bowlRegion[i++] = 32;
                        bowlRegion[i++] = 33;
                        bowlRegion[i++] = 34;
                        bowlRegion[i++] = 38;
                        bowlRegion[i++] = 39;
                        bowlRegion[i++] = 40;
                        bowlRegion[i++] = 41;
                        bowlRegion[i++] = 42;
                        bowlRegion[i++] = 43;
                    }
                    else
                    {
                        bowlRegion[i++] = 47;
                        bowlRegion[i++] = 48;
                        bowlRegion[i++] = 49;
                        bowlRegion[i++] = 50;
                        bowlRegion[i++] = 51;
                        bowlRegion[i++] = 56;
                        bowlRegion[i++] = 57;
                        bowlRegion[i++] = 58;
                        bowlRegion[i++] = 59;
                        bowlRegion[i++] = 60;
                        bowlRegion[i++] = 65;
                        bowlRegion[i++] = 66;
                        bowlRegion[i++] = 67;
                        bowlRegion[i++] = 68;
                        bowlRegion[i++] = 69;
                        bowlRegion[i++] = 74;
                        bowlRegion[i++] = 75;
                        bowlRegion[i++] = 76;
                        bowlRegion[i++] = 77;
                        bowlRegion[i++] = 78;
                    }

                    for (; i < 94; i++)
                        bowlRegion[i] = DefaultBowlingRegionList[rnd.Next(0, 30)];

                    for (; i < 100; i++)
                        bowlRegion[i] = DefaultBowlingWideRegionList[rnd.Next(0, 18)];

                    i = 0;

                }
                //Right hander to Left hander or Left hander to Right hander
                else if ((!BatsmanFacing.IsRightHandedBatsman && BowlerBowling.IsRightHandedBowler) || (BatsmanFacing.IsRightHandedBatsman && !BowlerBowling.IsRightHandedBowler))
                {
                    short x = Convert.ToInt16(100 - bowlSkillFunctionTemp);
                    short i = 0;

                    for (; i < x - 1; i++)
                        bowlRegion.Add(DefaultBowlingRegionAllBowlers[Convert.ToInt16(rnd.Next(0, 297))]);

                    i++;

                    if (_MRF > 87)
                    {
                        bowlRegion[i++] = 20;
                        bowlRegion[i++] = 21;
                        bowlRegion[i++] = 22;
                        bowlRegion[i++] = 23;
                        bowlRegion[i++] = 24;
                        bowlRegion[i++] = 25;
                        bowlRegion[i++] = 29;
                        bowlRegion[i++] = 30;
                        bowlRegion[i++] = 31;
                        bowlRegion[i++] = 32;
                        bowlRegion[i++] = 33;
                        bowlRegion[i++] = 34;
                        bowlRegion[i++] = 38;
                        bowlRegion[i++] = 39;
                        bowlRegion[i++] = 40;
                        bowlRegion[i++] = 41;
                        bowlRegion[i++] = 42;
                        bowlRegion[i++] = 43;
                    }
                    else
                    {
                        bowlRegion[i++] = 48;
                        bowlRegion[i++] = 49;
                        bowlRegion[i++] = 50;
                        bowlRegion[i++] = 51;
                        bowlRegion[i++] = 52;
                        bowlRegion[i++] = 57;
                        bowlRegion[i++] = 58;
                        bowlRegion[i++] = 59;
                        bowlRegion[i++] = 60;
                        bowlRegion[i++] = 61;
                        bowlRegion[i++] = 66;
                        bowlRegion[i++] = 67;
                        bowlRegion[i++] = 68;
                        bowlRegion[i++] = 69;
                        bowlRegion[i++] = 70;
                        bowlRegion[i++] = 75;
                        bowlRegion[i++] = 76;
                        bowlRegion[i++] = 77;
                        bowlRegion[i++] = 78;
                        bowlRegion[i++] = 79;
                    }

                    for (; i < 94; i++)
                        bowlRegion[i] = DefaultBowlingRegionList[rnd.Next(0, 30)];

                    for (; i < 100; i++)
                        bowlRegion[i] = DefaultBowlingWideRegionList[rnd.Next(0, 18)];

                    i = 0;

                }
            }
            else if (BowlerBowling.BowlerType == (short)_22yards.EnumDeclarations.BowlerType.OffSpin)
            {
                //Right hander to Right hander or Left hander to left hander
                if ((BatsmanFacing.IsRightHandedBatsman && BowlerBowling.IsRightHandedBowler) || (!BatsmanFacing.IsRightHandedBatsman && !BowlerBowling.IsRightHandedBowler))
                {
                    short x = Convert.ToInt16(100 - bowlSkillFunctionTemp);
                    short i = 0;

                    for (; i < x - 1; i++)
                        bowlRegion.Add(DefaultBowlingRegionAllBowlers[Convert.ToInt16(rnd.Next(0, 297))]);

                    i++;

                    if (_MRF > 87)
                    {
                        bowlRegion[i++] = 20;
                        bowlRegion[i++] = 21;
                        bowlRegion[i++] = 22;
                        bowlRegion[i++] = 23;
                        bowlRegion[i++] = 24;
                        bowlRegion[i++] = 25;
                        bowlRegion[i++] = 29;
                        bowlRegion[i++] = 30;
                        bowlRegion[i++] = 31;
                        bowlRegion[i++] = 32;
                        bowlRegion[i++] = 33;
                        bowlRegion[i++] = 34;
                        bowlRegion[i++] = 38;
                        bowlRegion[i++] = 39;
                        bowlRegion[i++] = 40;
                        bowlRegion[i++] = 41;
                        bowlRegion[i++] = 42;
                        bowlRegion[i++] = 43;
                    }
                    else
                    {
                        bowlRegion[i++] = 47;
                        bowlRegion[i++] = 48;
                        bowlRegion[i++] = 49;
                        bowlRegion[i++] = 50;
                        bowlRegion[i++] = 51;
                        bowlRegion[i++] = 56;
                        bowlRegion[i++] = 57;
                        bowlRegion[i++] = 58;
                        bowlRegion[i++] = 59;
                        bowlRegion[i++] = 60;
                        bowlRegion[i++] = 65;
                        bowlRegion[i++] = 66;
                        bowlRegion[i++] = 67;
                        bowlRegion[i++] = 68;
                        bowlRegion[i++] = 69;

                    }

                    for (; i < 94; i++)
                        bowlRegion[i] = DefaultBowlingRegionList[rnd.Next(0, 20)];

                    for (; i < 100; i++)
                        bowlRegion[i] = DefaultBowlingWideRegionList[rnd.Next(0, 16)];

                    i = 0;

                }
                //Right hander to Left hander or Left hander to Right hander
                else if ((!BatsmanFacing.IsRightHandedBatsman && BowlerBowling.IsRightHandedBowler) || (BatsmanFacing.IsRightHandedBatsman && !BowlerBowling.IsRightHandedBowler))
                {
                    short x = Convert.ToInt16(100 - bowlSkillFunctionTemp);
                    short i = 0;

                    for (; i < x - 1; i++)
                        bowlRegion.Add(DefaultBowlingRegionAllBowlers[Convert.ToInt16(rnd.Next(0, 297))]);

                    i++;

                    if (_MRF > 87)
                    {
                        bowlRegion[i++] = 20;
                        bowlRegion[i++] = 21;
                        bowlRegion[i++] = 22;
                        bowlRegion[i++] = 23;
                        bowlRegion[i++] = 24;
                        bowlRegion[i++] = 25;
                        bowlRegion[i++] = 29;
                        bowlRegion[i++] = 30;
                        bowlRegion[i++] = 31;
                        bowlRegion[i++] = 32;
                        bowlRegion[i++] = 33;
                        bowlRegion[i++] = 34;
                        bowlRegion[i++] = 38;
                        bowlRegion[i++] = 39;
                        bowlRegion[i++] = 40;
                        bowlRegion[i++] = 41;
                        bowlRegion[i++] = 42;
                        bowlRegion[i++] = 43;
                    }
                    else
                    {
                        bowlRegion[i++] = 48;
                        bowlRegion[i++] = 49;
                        bowlRegion[i++] = 50;
                        bowlRegion[i++] = 51;
                        bowlRegion[i++] = 52;
                        bowlRegion[i++] = 57;
                        bowlRegion[i++] = 58;
                        bowlRegion[i++] = 59;
                        bowlRegion[i++] = 60;
                        bowlRegion[i++] = 61;
                        bowlRegion[i++] = 66;
                        bowlRegion[i++] = 67;
                        bowlRegion[i++] = 68;
                        bowlRegion[i++] = 69;
                        bowlRegion[i++] = 70;

                    }
                    for (; i < 94; i++)
                        bowlRegion[i] = DefaultBowlingRegionList[rnd.Next(0, 20)];

                    for (; i < 100; i++)
                        bowlRegion[i] = DefaultBowlingWideRegionList[rnd.Next(0, 16)];

                    i = 0;

                }
            }
            else if (BowlerBowling.BowlerType == (short)_22yards.EnumDeclarations.BowlerType.LegSpin)
            {
                //Right hander to Right hander or Left hander to left hander
                if ((BatsmanFacing.IsRightHandedBatsman && BowlerBowling.IsRightHandedBowler) || (!BatsmanFacing.IsRightHandedBatsman && !BowlerBowling.IsRightHandedBowler))
                {
                    short x = Convert.ToInt16(100 - bowlSkillFunctionTemp);
                    short i = 0;

                    for (; i < x - 1; i++)
                        bowlRegion.Add(DefaultBowlingRegionAllBowlers[Convert.ToInt16(rnd.Next(0, 297))]);

                    i++;

                    if (_MRF > 87)
                    {
                        bowlRegion[i++] = 20;
                        bowlRegion[i++] = 21;
                        bowlRegion[i++] = 22;
                        bowlRegion[i++] = 23;
                        bowlRegion[i++] = 24;
                        bowlRegion[i++] = 25;
                        bowlRegion[i++] = 29;
                        bowlRegion[i++] = 30;
                        bowlRegion[i++] = 31;
                        bowlRegion[i++] = 32;
                        bowlRegion[i++] = 33;
                        bowlRegion[i++] = 34;
                        bowlRegion[i++] = 38;
                        bowlRegion[i++] = 39;
                        bowlRegion[i++] = 40;
                        bowlRegion[i++] = 41;
                        bowlRegion[i++] = 42;
                        bowlRegion[i++] = 43;
                    }
                    else
                    {
                        bowlRegion[i++] = 48;
                        bowlRegion[i++] = 49;
                        bowlRegion[i++] = 50;
                        bowlRegion[i++] = 51;
                        bowlRegion[i++] = 52;
                        bowlRegion[i++] = 57;
                        bowlRegion[i++] = 58;
                        bowlRegion[i++] = 59;
                        bowlRegion[i++] = 60;
                        bowlRegion[i++] = 61;
                        bowlRegion[i++] = 66;
                        bowlRegion[i++] = 67;
                        bowlRegion[i++] = 68;
                        bowlRegion[i++] = 69;
                        bowlRegion[i++] = 70;
                    }

                    for (; i < 94; i++)
                        bowlRegion[i] = DefaultBowlingRegionList[rnd.Next(0, 20)];

                    for (; i < 100; i++)
                        bowlRegion[i] = DefaultBowlingWideRegionList[rnd.Next(0, 20)];

                    i = 0;

                }
                //Right hander to Left hander or Left hander to Right hander
                else if ((!BatsmanFacing.IsRightHandedBatsman && BowlerBowling.IsRightHandedBowler) || (BatsmanFacing.IsRightHandedBatsman && !BowlerBowling.IsRightHandedBowler))
                {
                    short x = Convert.ToInt16(100 - bowlSkillFunctionTemp);
                    short i = 0;

                    for (; i < x - 1; i++)
                        bowlRegion.Add(DefaultBowlingRegionAllBowlers[Convert.ToInt16(rnd.Next(0, 297))]);

                    i++;

                    if (_MRF > 87)
                    {
                        bowlRegion[i++] = 20;
                        bowlRegion[i++] = 21;
                        bowlRegion[i++] = 22;
                        bowlRegion[i++] = 23;
                        bowlRegion[i++] = 24;
                        bowlRegion[i++] = 25;
                        bowlRegion[i++] = 29;
                        bowlRegion[i++] = 30;
                        bowlRegion[i++] = 31;
                        bowlRegion[i++] = 32;
                        bowlRegion[i++] = 33;
                        bowlRegion[i++] = 34;
                        bowlRegion[i++] = 38;
                        bowlRegion[i++] = 39;
                        bowlRegion[i++] = 40;
                        bowlRegion[i++] = 41;
                        bowlRegion[i++] = 42;
                        bowlRegion[i++] = 43;
                    }
                    else
                    {
                        bowlRegion[i++] = 48;
                        bowlRegion[i++] = 49;
                        bowlRegion[i++] = 50;
                        bowlRegion[i++] = 51;
                        bowlRegion[i++] = 52;
                        bowlRegion[i++] = 57;
                        bowlRegion[i++] = 58;
                        bowlRegion[i++] = 59;
                        bowlRegion[i++] = 60;
                        bowlRegion[i++] = 61;
                        bowlRegion[i++] = 66;
                        bowlRegion[i++] = 67;
                        bowlRegion[i++] = 68;
                        bowlRegion[i++] = 69;
                        bowlRegion[i++] = 70;
                    }

                    for (; i < 94; i++)
                        bowlRegion[i] = DefaultBowlingRegionList[rnd.Next(0, 20)];

                    for (; i < 100; i++)
                        bowlRegion[i] = DefaultBowlingWideRegionList[rnd.Next(0, 20)];

                    i = 0;
                }
            }

            #endregion

            //another pass
            List <short> bowlRegion2 = new List<short>();
            for (int i=0; i < 99; i++)
                bowlRegion2.Add(bowlRegion[(short)(rnd.Next(0, 100))]);

            bowlRegion.Clear();

            short IntendedBowlRegion = bowlRegion2[rnd.Next(0, 100)];
            bowlRegion2.Clear();

            short Deviationfactor = Convert.ToInt16((100 - bowlSkillFunctionTemp) / 10);
            Deviationfactor      /= 2;
            Deviationfactor       = (Deviationfactor > Convert.ToInt16(1)) ? Convert.ToInt16(1) : Deviationfactor;
            
            //0 - ft. left diagonal, 1 - front, 2 - Ft. right Diagonal, 3-right, 4 - back rt. diagonal, 5- back, 6 - back lt. diagonal, 7 - left.
            short deviationangle     = (short)rnd.Next(0, 8);
            short FinalBowlingRegion = IntendedBowlRegion;

            switch (deviationangle)
            {
                case 0:
                    if (IntendedBowlRegion >= 1 && IntendedBowlRegion <= 9)
                        break;
                    if (IntendedBowlRegion % 9 == 1)
                        break;
                    FinalBowlingRegion = Convert.ToInt16(IntendedBowlRegion - 10);
                    break;
                case 1:
                    if (IntendedBowlRegion >= 1 && IntendedBowlRegion <= 9)
                        break;
                    FinalBowlingRegion = Convert.ToInt16(IntendedBowlRegion - 9);
                    break;
                case 2:
                    if (IntendedBowlRegion >= 1 && IntendedBowlRegion <= 9)
                        break;
                    if (IntendedBowlRegion % 9 == 0)
                        break;
                    FinalBowlingRegion = Convert.ToInt16(IntendedBowlRegion - 8);
                    break;
                case 3:
                    if (IntendedBowlRegion % 9 == 0)
                        break;
                    FinalBowlingRegion = Convert.ToInt16(IntendedBowlRegion + 1);
                    break;
                case 4:
                    if (IntendedBowlRegion >= 109 && IntendedBowlRegion <= 117)
                        break;
                    if (IntendedBowlRegion % 9 == 0)
                        break;
                    FinalBowlingRegion = Convert.ToInt16(IntendedBowlRegion + 10);
                    break;
                case 5:
                    if (IntendedBowlRegion >= 109 && IntendedBowlRegion <= 117)
                        break;
                    FinalBowlingRegion = Convert.ToInt16(IntendedBowlRegion + 9);
                    break;
                case 6:
                    if (IntendedBowlRegion >= 109 && IntendedBowlRegion <= 117)
                        break;
                    if (IntendedBowlRegion % 9 == 1)
                        break;
                    FinalBowlingRegion = Convert.ToInt16(IntendedBowlRegion + 8);
                    break;
                case 7:
                    if (IntendedBowlRegion % 9 == 1)
                        break;
                    FinalBowlingRegion = Convert.ToInt16(IntendedBowlRegion - 1);
                    break;
            }

            return FinalBowlingRegion;
        }

        /// <summary>
        /// Generates the run up angle.
        /// </summary>
        /// <param name="_MRF">The MatchRiskFactor.</param>
        /// <returns>Enum Runup Angle</returns>
        public int GenerateRunUpAngle(short _MRF)
        {
            if (BowlingTeam == null || BowlerBowling == null)
                return -1;

            if (BowlingTeam.IsUserSelected)
                return BowlerBowling.BowlingRunupAngle;

            if (_MRF > 87 || (BowlerBowling.CurrentBowlingConfidence < 50))
                return (short)(rnd.Next(1, 7));

            if (BowlerBowling.IsRightHandedBowler)
                return (short)_22yards.EnumDeclarations.BowlerRunUpAngle.OverNormal;

            return (short)_22yards.EnumDeclarations.BowlerRunUpAngle.RoundNormal;
        }

        /// <summary>
        /// Generates the length of the run up. If greater than default runup length, then its a no ball
        /// </summary>
        /// <returns>The generated Runup Length as a float</returns>
        public float GenerateRunUpLength()
        {
            if (BowlerBowling == null)
                return -1.0f;
            float RunUpLength = BowlerBowling.BowlingRunupLength;
            int lowerLimit    = (BowlerBowling.Stamina + BowlerBowling.Strength)/2;
            RunUpLength       = BowlerBowling.BowlingRunupLength * ( ( (float)rnd.Next(lowerLimit, 102) + (float)rnd.NextDouble() ) / (float)100 );

            return RunUpLength;
        }

        /// <summary>
        /// Generates the bowling speed of bowler.
        /// </summary>
        /// <param name="_MRF">The MatchRiskFactor.</param>
        /// <param name="AlternateBall">if set to <c>true</c> [bowl slower ball]. This is set from User window</param>
        /// <returns>The Speed as float as Km/Hr</returns>
        public float GenerateBowlingSpeed(short _MRF, float _RunupLength, bool AlternateBall = false)
        {
            if (BowlerBowling == null)
                return -1.0f;
            List<short> tmpList = new List<short>();
            float Pace          = BowlerBowling.UpperBowlingSpeedLimit;
            short lowerPoint    = (short)(((BowlerBowling.Strength + BowlerBowling.Stamina) / 2) - 10);
            Pace                = BowlerBowling.UpperBowlingSpeedLimit * (((float)(rnd.Next(lowerPoint, 101) + (float)rnd.NextDouble()) / (float)100));

            if (BowlerBowling.BowlerType == (short)_22yards.EnumDeclarations.BowlerType.Fast ||
                BowlerBowling.BowlerType == (short)_22yards.EnumDeclarations.BowlerType.FastMedium ||
                BowlerBowling.BowlerType == (short)_22yards.EnumDeclarations.BowlerType.MediumFast)
            {
                if (AlternateBall)
                    return Pace - Pace / 4;

                for (int i = 0; i < SlowBallProbability; i++)
                    tmpList.Add((short)0);
                for (int i = SlowBallProbability; i < 99; i++)
                    tmpList.Add((short)1);

                if (_MRF > 87 || BowlerBowling.CurrentBowlingConfidence > 80 || BowlerBowling.CurrentBowlingConfidence < 50)
                    for (int i = 90; i < 99; i++)
                        tmpList[i] = 0;

                if (tmpList[rnd.Next(0, 100)] == 0) //SlowBall
                    Pace = Pace - (Pace / 4);

            }
            else if (BowlerBowling.BowlerType == (short)_22yards.EnumDeclarations.BowlerType.SlowMedium)
            {
                if (AlternateBall)
                    return Pace + Pace / 4;

                for (int i = 0; i < FastBallProbability; i++)
                    tmpList.Add((short)1);
                for (int i = FastBallProbability; i < 99; i++)
                    tmpList.Add((short)0);

                if (_MRF > 87 || BowlerBowling.CurrentBowlingConfidence > 80 || BowlerBowling.CurrentBowlingConfidence < 50)
                    for (int i = 90; i < 99; i++)
                        tmpList[i] = 1;

                if (tmpList[rnd.Next(0, 100)] == 1) //FastBall
                    Pace = Pace + (Pace / 4);

            }
            else if (BowlerBowling.BowlerType == (short)_22yards.EnumDeclarations.BowlerType.OffSpin)
            {
                if (AlternateBall)
                    return Pace + Pace / 4;

                for (int i = 0; i < FastBallProbability; i++)
                    tmpList.Add((short)1);
                for (int i = FastBallProbability; i < 99; i++)
                    tmpList.Add((short)0);

                if (_MRF > 87 || BowlerBowling.CurrentBowlingConfidence > 80 || BowlerBowling.CurrentBowlingConfidence < 50)
                    for (int i = 90; i < 99; i++)
                        tmpList[i] = 1;

                if (tmpList[rnd.Next(0, 100)] == 1) //FastBall
                    Pace = Pace + (Pace / 4);
            }
            else if (BowlerBowling.BowlerType == (short)_22yards.EnumDeclarations.BowlerType.LegSpin)
            {
                if (AlternateBall)
                    return Pace + Pace / 4;

                for (int i = 0; i < FastBallProbability; i++)
                    tmpList.Add((short)1);
                for (int i = FastBallProbability; i < 99; i++)
                    tmpList.Add((short)0);

                if (_MRF > 87 || BowlerBowling.CurrentBowlingConfidence > 80 || BowlerBowling.CurrentBowlingConfidence < 50)
                    for (int i = 90; i < 99; i++)
                        tmpList[i] = 1;

                if (tmpList[rnd.Next(0, 100)] == 1) //FastBall
                    Pace = Pace + (Pace / 4);
            }
            
            return Pace;
        } //End of GenerateBowlingSpeed


    } //End of class

} //End of Namespace
